package com.gitee.feizns.explore.data_structure.tree.bs.easy;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @author feizns
 * @since 2020/1/4
 */
public class NTreePostOrder {

    public static void main(String[] args) {

    }

    public static List<Integer> postOrder(Node root) {
        List<Integer> ret = new LinkedList<>();
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while ( !stack.isEmpty() ) {
            Node pop = stack.pop();
            ret.add(pop.val);
            if ( pop.children != null && !pop.children.isEmpty() ) {
                for (int i = 0; i < pop.children.size(); i++)
                    stack.push(pop.children.get(i));
            }
        }
        Collections.reverse(ret);
        return ret;
    }

}
